home *** CD-ROM | disk | FTP | other *** search
/ Info-Mac 4 / Info_Mac IV CD-ROM (Pacific HiTech Inc.)(August 1994).iso / Development / Source / MSG Demo 1.4.source Folder / Demo ƒ / Shell ƒ / apple events.c < prev    next >
Text File  |  1994-04-22  |  5KB  |  155 lines

  1. /**********************************************************************\
  2.  
  3. File:        apple events.c
  4.  
  5. Purpose:    This module handles the 4 required apple events: open
  6.             application, open document, print document (not supported),
  7.             and quit application.
  8.  
  9. This program is free software; you can redistribute it and/or modify
  10. it under the terms of the GNU General Public License as published by
  11. the Free Software Foundation; either version 2 of the License, or
  12. (at your option) any later version.
  13.  
  14. This program is distributed in the hope that it will be useful,
  15. but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17. GNU General Public License for more details.
  18.  
  19. You should have received a copy of the GNU General Public License
  20. along with this program in a file named "GNU General Public License".
  21. If not, write to the Free Software Foundation, 675 Mass Ave,
  22. Cambridge, MA 02139, USA.
  23.  
  24. \**********************************************************************/
  25.  
  26. #include "apple events.h"
  27. #include "environment.h"
  28. #include "demo generic open.h"
  29.  
  30. /*-----------------------------------------------------------------------------------*/
  31. /* internal stuff for apple events.c                                                 */
  32. /* (need to be declared here because SetUpAppleEvents() references them              */
  33.  
  34. pascal OSErr HandleOpenAppAE(const AppleEvent *theAppleEvent, const AppleEvent *reply,
  35.     long refcon);
  36. pascal OSErr HandleOpenDocAE(const AppleEvent *theAppleEvent, const AppleEvent *reply,
  37.     long refcon);
  38. pascal OSErr HandlePrintDocAE(const AppleEvent *theAppleEvent, const AppleEvent *reply,
  39.     long refcon);
  40. pascal OSErr HandleQuitAE(const AppleEvent *theAppleEvent, const AppleEvent *reply,
  41.     long refcon);
  42. pascal OSErr MyGotRequiredParams(const AppleEvent *theAppleEvent);
  43.  
  44. void SetUpAppleEvents(void)
  45. /* called at program initialization, AFTER checking if apple events exist at all.
  46.    This is the magic linker that makes the procedures below get called when we
  47.    say AEProcessAppleEvent(&theEvent) in response to a kHighLevelEvent (see main.c). */
  48.  
  49. {
  50.     AEInstallEventHandler(kCoreEventClass, kAEOpenApplication,
  51.                                     HandleOpenAppAE, 0, FALSE);
  52.     AEInstallEventHandler(kCoreEventClass, kAEOpenDocuments,
  53.                                     HandleOpenDocAE, 0, FALSE);
  54.     AEInstallEventHandler(kCoreEventClass, kAEPrintDocuments,
  55.                                     HandlePrintDocAE, 0, FALSE);
  56.     AEInstallEventHandler(kCoreEventClass, kAEQuitApplication,
  57.                                     HandleQuitAE, 0, FALSE);
  58.     AESetInteractionAllowed(kAEInteractWithAll);    /* don't know what this does, sorry */
  59. }
  60.  
  61. /* Don't ask about the rest of this code.  It is a straight port from the pascal
  62.    code given in IM Interapplication Communication; not even the variable names
  63.    have been changed to protect the innocent.  I don't pretend to understand this,
  64.    but it works well enough to (1) not crash, (2) open and close the application
  65.    properly, and (3) get an FSSpec on open/print which we can then work on
  66.    directly (see the generic open.c file). */
  67.  
  68. pascal OSErr HandleOpenAppAE(const AppleEvent *theAppleEvent, const AppleEvent *reply,
  69.     long refcon)
  70. {
  71.     return MyGotRequiredParams(theAppleEvent);
  72. }
  73.  
  74. pascal OSErr HandleOpenDocAE(const AppleEvent *theAppleEvent, const AppleEvent *reply,
  75.     long refcon)
  76. {
  77.     OSErr            isHuman, dummy;
  78.     FSSpec            myFSS;
  79.     AEDescList        docList;
  80.     long            index, itemsInList;
  81.     Size            actualSize;
  82.     AEKeyword        keywd;
  83.     DescType        returnedType;
  84.     
  85.     isHuman=AEGetParamDesc(theAppleEvent, keyDirectObject, typeAEList, &docList);
  86.     if (isHuman==noErr)
  87.     {
  88.         if (MyGotRequiredParams(theAppleEvent)==noErr)
  89.         {
  90.             if (AECountItems(&docList, &itemsInList)==noErr)
  91.             {
  92.                 for (index=1; index<=itemsInList; index++)
  93.                 {
  94.                     if (AEGetNthPtr(&docList, index, typeFSS, &keywd, &returnedType,
  95.                         &myFSS, sizeof(myFSS), &actualSize)==noErr)
  96.                     {
  97.                         GenericOpen(&myFSS);        /* in rc generic open.c */
  98.                     }
  99.                     else
  100.                     {
  101.                         // handle error getting Nth pointer
  102.                     }
  103.                 }
  104.             }
  105.             else
  106.             {
  107.                 // handle error counting items
  108.             }
  109.         }
  110.         else
  111.         {
  112.             // handle error from MyGetRequiredParams
  113.             dummy=AEDisposeDesc(&docList);
  114.         }
  115.     }
  116.     else
  117.     {
  118.         // handle error getting direct parameter
  119.     }
  120.     
  121.     return isHuman;
  122. }
  123.  
  124. pascal OSErr HandlePrintDocAE(const AppleEvent *theAppleEvent, const AppleEvent *reply,
  125.     long refcon)
  126. {
  127.     return errAEEventNotHandled;
  128. }
  129.  
  130. pascal OSErr HandleQuitAE(const AppleEvent *theAppleEvent, const AppleEvent *reply,
  131.     long refcon)
  132. {
  133.     OSErr            isHuman;
  134.     
  135.     isHuman=MyGotRequiredParams(theAppleEvent);
  136.     if (isHuman==noErr)
  137.         gDone=TRUE;    /* our global program-done flag, so we will exit our event loop */
  138.                     /* do NOT under any circumstances call ExitToShell() here; */
  139.                     /* be patient and wait to get back to the event loop, which */
  140.                     /* will exit the next time around now than gDone=TRUE */
  141.     
  142.     return isHuman;
  143. }
  144.  
  145. pascal OSErr MyGotRequiredParams(const AppleEvent *theAppleEvent)
  146. {
  147.     DescType        returnedType;
  148.     Size            actualSize;
  149.     
  150.     /* yeah, whatever */
  151.     return (AEGetAttributePtr(theAppleEvent, keyMissedKeywordAttr, typeWildCard,
  152.             &returnedType, 0L, 0, &actualSize)==errAEDescNotFound) ? noErr :
  153.             errAEParamMissed;
  154. }
  155.